<?php namespace Phpcmf\Controllers\Member;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Jiaquan extends \Phpcmf\Home\Module
{
	public function __construct() {
        parent::__construct();
    }

	public function index() {
		//判断是post
		if (IS_POST) {
			//get 参数
			$id = (int)\Phpcmf\Service::L('input')->post('id'); //圈子id
			$shuju_data = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $id)->where('status', 1)->get()->getRowArray();
			//标题
			if($shuju_data['title']){
				$zantitle = $shuju_data['title'];
			} else {
				$zantitle = dr_strcut(dr_clearhtml($shuju_data['neirong']), 20);
			}
			
			
			
			// 判断 
			$jiaquan_data = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $id)->where('uid', $this->uid)->countAllResults();
			if($jiaquan_data){
				// 已经加入了 那么就删除
				\Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $id)->where('uid', $this->uid)->delete();
				$jiaquan_tongji = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $id)->countAllResults();
				//更新加入人数
				\Phpcmf\Service::M()->db->table('1_quan')->where('id', $id)->update(['jiaru_total'=>$jiaquan_tongji]);
				$data = [
					'code' => 0,
					'data' => $jiaquan_tongji,
					'uid'=> $this->uid,
	
				];

				$this->_json('0', '已退出',$data);
			} else {
				// 插入数据
				$insert_data = [
					'quanid' => $id,
					'uid' => $this->uid,
					'shenhe' => 1,
					'tianjiashijian' => time(),
				];
				$insert_id = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->insert($insert_data);
				//更新加入人数
				$jiaquan_tongji = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $id)->countAllResults();
				\Phpcmf\Service::M()->db->table('1_quan')->where('id', $id)->update(['jiaru_total'=>$jiaquan_tongji]);
				if($insert_id){
					$jiaquan_tongji = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $id)->countAllResults();
					$data = [
						'code' => 1,
						'data' => $jiaquan_tongji,
						'uid'=> $this->uid,
		
					];
					$dataduixin = [
						'catid'=> 18, //类型
						'title' => '加入了您的圈子', //标题
						'zhiding' => '', //置顶
						'content'=> $zantitle, //通知内容
						'uid'=> $this->uid, //发布人的uid
						'faxinren'=> $this->uid, //发布人的uid
						'shouxinren'=> (int)$shuju_data['uid'],//收信人
						'tupian'=> '', //图片
						'shipin'=> '', //视频
						'yuyin'=> '', //语音
						'fujian'=> '', //附件
						'yidu'=> 0, //已读
						'quanziid'=> $id, //圈子ID
						'quantieid'=> '', //圈帖ID
						'url' => '',
					];
					if($this->uid != (int)$shuju_data['uid']){
						// 通知
						\Phpcmf\Service::M('auth', 'duixin')->addtongzhi($dataduixin);
					}

	
					$this->_json('1', '加入成功',$data);
				}
			}
			
		} else {
			$data = [
				'code' => 0,
				'data' => '无效提交',
				'uid'=> 0,

			];

			$this->_json('0', '无效提交');
		}

	}
	public function  jiaquan_panduan() {
		//get 参数
		$id = (int)\Phpcmf\Service::L('input')->get('id'); //帖子id
		
		$shoucang_data = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $id)->where('uid', $this->uid)->countAllResults();
		$shoucang_tongji = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $id)->countAllResults();
		if($shoucang_data){
			$data = [
				'code' => 1,
				'data' => $shoucang_tongji,
				'uid'=> $this->uid,

			];

			
			$this->_json('1', '已加入',$data);
		} else {
			$data = [
				'code' => 0,
				'data' => $shoucang_tongji,
				'uid'=> $this->uid,
			];
			$this->_json('0', '未加入',$data);
		}
		
		
	}
	

}
